/** 
 * @file yfrobot
 * @brief YFROBOT's sensors Mind+ library.
 * @n This is a MindPlus graphics programming extension for YFROBOT's module.
 * 
 * @copyright    YFROBOT,2024
 * @copyright    MIT Lesser General Public License
 * 
 * @author [email](yfrobot@qq.com)
 * @date  2024-11-17
*/

enum ReadBlockNUM {
    //% block="1"
    1,
    //% block="2"
    2,
    //% block="4"
    4,
    //% block="5"
    5,
    //% block="6"
    6,
    //% block="8"
    8,
    //% block="9"
    9,
    //% block="10"
    10,
    //% block="12"
    12,
    //% block="13"
    13,
    //% block="14"
    14,
    //% block="16"
    16,
    //% block="17"
    17,
    //% block="18"
    18,
    //% block="20"
    20,
    //% block="21"
    21,
    //% block="22"
    22,
    //% block="24"
    24,
    //% block="25"
    25,
    //% block="26"
    26,
    //% block="28"
    28,
    //% block="29"
    29,
    //% block="30"
    30,
    //% block="32"
    32,
    //% block="33"
    33,
    //% block="34"
    34,
    //% block="36"
    36,
    //% block="37"
    37,
    //% block="38"
    38,
    //% block="40"
    40,
    //% block="41"
    41,
    //% block="42"
    42,
    //% block="44"
    44,
    //% block="45"
    45,
    //% block="46"
    46,
    //% block="48"
    48,
    //% block="49"
    49,
    //% block="50"
    50,
    //% block="52"
    52,
    //% block="53"
    53,
    //% block="54"
    54,
    //% block="56"
    56,
    //% block="57"
    57,
    //% block="58"
    58,
    //% block="60"
    60,
    //% block="61"
    61
}

enum Block_BYTE_NUM {
    //% block="0"
    0,
    //% block="1"
    1,
    //% block="2"
    2,
    //% block="3"
    3,
    //% block="4"
    4,
    //% block="5"
    5,
    //% block="6"
    6,
    //% block="7"
    7,
    //% block="8"
    8,
    //% block="9"
    9,
    //% block="10"
    10,
    //% block="11"
    11,
    //% block="12"
    12,
    //% block="13"
    13,
    //% block="14"
    14,
    //% block="15"
    15
}

enum VALUE_Block {
    //% block="Increment"
    Increment,
    //% block="Decrement"
    Decrement
}

//% color="#c3cbd0" iconWidth=40 iconHeight=40
namespace rfid {
    //% block="RFID initliallize" blockType="command"
    export function init(parameter: any, block: any) {
        Generator.addInclude("include_rfid_iic", `#include "YFROBOTRFIDI2C.h"`);
        Generator.addObject("rfid_Object", "YFROBOTRFID", `yf_rfid; `);
        // Generator.addObject("include_rfid_iic_key", `YFROBOTRFID::MIFARE_Key`, `key;`);
        Generator.addObject("include_rfid_iic_status", `YFROBOTRFID::StatusCode`, `status;`);
        Generator.addSetup(`initSetup_wire`, `Wire.begin();`);
        Generator.addSetup(`initSetup_rfid`, `yf_rfid.PCD_Init();`);
    }

    //% block="Look for new card" blockType="boolean"
    export function ScanCard(parameter: any, block: any) {
        Generator.addCode(`(yf_rfid.PICC_IsNewCardPresent()==YFROBOTRFID::STATUS_OK && yf_rfid.PICC_ReadCardSerial()==YFROBOTRFID::STATUS_OK)`);
    }

    //% block="Card UID is [UID]" blockType="boolean"
    //% UID.shadow="string" UID.defl="2568daab"
    export function ScanCardUID(parameter: any, block: any) {
        let uid = parameter.UID.code;
        Generator.addCode(`( yf_rfid.IsCardUid(${uid}) )`);
    }


    //% block="RFID Print UID about the card(String)" blockType="reporter"
    export function PrintUid(parameter: any, block: any) {
        Generator.addCode(`yf_rfid.readUid()`);
    }

    
    //% block="---"
    export function noteSep() { }

    //% block="RFID Print Info about the card" blockType="command"
    export function PrintInfo(parameter: any, block: any) {
        Generator.addCode(`yf_rfid.PICC_DumpToSerial(&(yf_rfid.uid));`);
    }


    //% block="Read the card data block [BLOCK] 16bit(String)" blockType="reporter"
    //% BLOCK.shadow="dropdownRound"   BLOCK.options="ReadBlockNUM"     BLOCK.defl="ReadBlockNUM.0"
    export function ReadDataBlock(parameter: any, block: any) {
        let readBlockNum = parameter.BLOCK.code;
        Generator.addCode(`yf_rfid.GetBlockData(${readBlockNum})`);
    }

    //% block="Read the card data block [BLOCK] [BIT](Number)" blockType="reporter"
    //% BLOCK.shadow="dropdownRound"   BLOCK.options="ReadBlockNUM"     BLOCK.defl="ReadBlockNUM.0"
    //% BIT.shadow="dropdownRound"   BIT.options="Block_BYTE_NUM"     BIT.defl="Block_BYTE_NUM.0"
    export function ReadDataBlockBit(parameter: any, block: any) {
        let readBlockNum = parameter.BLOCK.code;
        let bit = parameter.BIT.code;
        Generator.addCode(`yf_rfid.GetBit(${readBlockNum},${bit})`);
    }

    //% block="Write the card data block [BLOCK] [BIT] [VALUE]" blockType="command"
    //% BLOCK.shadow="dropdownRound"   BLOCK.options="ReadBlockNUM"     BLOCK.defl="ReadBlockNUM.0"
    //% BIT.shadow="dropdownRound"   BIT.options="Block_BYTE_NUM"     BIT.defl="Block_BYTE_NUM.0"
    //% VALUE.shadow="range"   VALUE.params.min=0    VALUE.params.max=255    VALUE.defl=100
    export function WriteDataBlockBit(parameter: any, block: any) {
        let readBlockNum = parameter.BLOCK.code;
        let bit = parameter.BIT.code;
        let value = parameter.VALUE.code;
        Generator.addCode(`yf_rfid.WriteBlockDataBit(${readBlockNum},${bit},${value});`);
    }
    //% block="---"
    export function noteSeps() { }

    // 块62作为固定的值块，电子钱包使用

    //% block="Initialize the wallet and clear it to 0" blockType="command"
    export function InitValueBlock(parameter: any, block: any) {
        Generator.addCode(`yf_rfid.InitValueBlock();`);
    }
    
    //% block="Get wallet balance" blockType="reporter"
    export function ReadValueBlock(parameter: any, block: any) {
        Generator.addCode(`yf_rfid.ReadValueBlock()`);
    }

    //% block="Give wallet [VALUEBLOCK] amount [VALUE]" blockType="command"
    //% VALUEBLOCK.shadow="dropdownRound"   VALUEBLOCK.options="VALUE_Block"     VALUEBLOCK.defl="VALUE_Block.Increment"
    //% VALUE.shadow="number"   VALUE.defl=100
    export function ValueBlock(parameter: any, block: any) {
        let VBlock = parameter.VALUEBLOCK.code;
        let value = parameter.VALUE.code;
        Generator.addCode(`yf_rfid.${VBlock}(${value});`);
    }

    //% block="exit response" blockType="command"
    export function PCD_Stop(parameter: any, block: any) {
        Generator.addCode(`yf_rfid.PCD_Stop();`);
    }
}
